﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TSV.Models;
using TSV.Library.Common;
using System.Data.Common;
using System.Collections;
namespace TSV.Areas.Administration.Models
{
    public class AdvertisingDao : BaseDao
    {
        #region "Get"
        public List<sp_GetAdvertisingResult> GetList()
        {
            return dbContext.sp_GetAdvertising().ToList();

        }

        public Advertising GetById(string id)
        {
            return dbContext.Advertisings.Where(c => c.ID.ToString().Equals(id)).SingleOrDefault<Advertising>();
        }
        public List<sp_GetAdvertisingResult> Sort(List<sp_GetAdvertisingResult> list, string sortColumn, string sortOrder)
        {
            int order;

            if (sortOrder == "desc")
            {
                order = -1;
            }
            else
            {
                order = 1;
            }
            switch (sortColumn)
            {

                case "AdOrder":
                    list.Sort(
                         delegate(sp_GetAdvertisingResult m1, sp_GetAdvertisingResult m2)
                         { return m1.AdOrder.CompareTo(m2.AdOrder) * order; });
                    break;
                case "Title":
                    list.Sort(
                         delegate(sp_GetAdvertisingResult m1, sp_GetAdvertisingResult m2)
                         { return m1.Title.CompareTo(m2.Title) * order; });
                    break;
                case "IsIntenalLink":
                    list.Sort(
                         delegate(sp_GetAdvertisingResult m1, sp_GetAdvertisingResult m2)
                         { return m1.IsIntenalLink.ToString().CompareTo(m2.IsIntenalLink.ToString()) * order; });
                    break;
                case "ExternalLink":
                    list.Sort(
                         delegate(sp_GetAdvertisingResult m1, sp_GetAdvertisingResult m2)
                         { return m1.ExternalLink.CompareTo(m2.ExternalLink) * order; });
                    break;
                case "Position":
                    list.Sort(
                         delegate(sp_GetAdvertisingResult m1, sp_GetAdvertisingResult m2)
                         { return m1.Position.CompareTo(m2.Position) * order; });
                    break;
                
            }

            return list;
        }
        #endregion
        public Message Insert(Advertising objUI)
        {
            Message msg = null;
            try
            {
                if (objUI != null)
                {
                    // Set more info                    

                    dbContext.Advertisings.InsertOnSubmit(objUI);
                    dbContext.SubmitChanges();

                    // Show success message
                    msg = new Message(MessageConstants.I0001, MessageType.Info, "Một tin quảng cáo", "thêm");
                }
            }
            catch (Exception)
            {
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }
            return msg;
        }
        public Message DeleteList(string ids)
        {
            Message msg = null;
            DbTransaction trans = null;
            bool canDelete = true;
            try
            {
                dbContext.Connection.Open();
                trans = dbContext.Connection.BeginTransaction();
                dbContext.Transaction = trans;

                if (!string.IsNullOrEmpty(ids))
                {
                    ids = ids.TrimEnd(',');
                    string[] idArr = ids.Split(',');
                   
                    foreach (string id in idArr)
                    {
                        Advertising oscObj = GetById(id);
                        if (oscObj != null)
                            dbContext.Advertisings.DeleteOnSubmit(oscObj);
                    }
                    dbContext.SubmitChanges();
                    
                    if (canDelete)
                    {
                        // Show succes message
                        msg = new Message(MessageConstants.I0001, MessageType.Info, idArr.Count().ToString() + " Record(s)", "xóa");
                        trans.Commit();
                    }
                    else
                    {
                        trans.Rollback();
                    }
                }
            }
            catch (Exception)
            {
                if (trans != null) trans.Rollback();
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
                
            }
            return msg;
        }
        public Message Update(Advertising objUI)
        {
            Message msg = null;
            try
            {
                if (objUI != null)
                {
                    // Get current group in dbContext
                    Advertising objDb = GetById(objUI.ID.ToString());

                    if (objDb != null)
                    {
                        // Update info by objUI
                        objDb.Title = objUI.Title;
                        objDb.Image = objUI.Image;
                        //objDb.AdOrder = objUI.AdOrder;
                        objDb.Description = objUI.Description;
                        objDb.ExternalLink = objUI.ExternalLink;
                        objDb.IsIntenalLink = objUI.IsIntenalLink;
                        objDb.Position = objUI.Position;
                        // Submit changes to dbContext
                        dbContext.SubmitChanges();
                        // Show success message
                        msg = new Message(MessageConstants.I0001, MessageType.Info, "Bài quảng cáo '" + objUI.Title + "'", "cập nhật");

                    }
                }
            }
            catch (Exception)
            {
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }

            return msg;
        }

        public List<Advertising> GetListBigPartner()
        {
            return dbContext.Advertisings.Where(q => q.Position == "T").OrderByDescending(q => q.AdOrder).ToList();
        }

        public List<Advertising> GetAdvertisingLeft()
        {
            return dbContext.Advertisings.Where(q => q.Position == "L").OrderByDescending(q => q.AdOrder).ToList();
        }

        public List<Advertising> GetAdvertisingBottom()
        {
            return dbContext.Advertisings.Where(q => q.Position == "B").OrderByDescending(q => q.AdOrder).ToList();
        }
        public Advertising GetByOrder(int order)
        {
            return dbContext.Advertisings.Where(c => c.AdOrder == order).FirstOrDefault<Advertising>();
        }
        public Message MoveUp(Advertising objUI)
        {
            Message msg = null;
            try
            {
                if (objUI != null)
                {
                    // Get current group in dbContext
                    Advertising objCurrent = objUI; //GetByOrder(objUI.AdOrder);
                    Advertising objNext = GetByOrder(objUI.AdOrder + 1);

                    if (objCurrent != null && objNext != null)
                    {
                        int currentOrder = objCurrent.AdOrder;
                        int nextOrder = objNext.AdOrder;
                        // Update info by objUI
                        objCurrent.AdOrder = nextOrder;
                        objNext.AdOrder = currentOrder;

                        // Submit changes to dbContext
                        dbContext.SubmitChanges();
                        // Show success message
                        msg = new Message(MessageConstants.I0001, MessageType.Info, "Bài quảng cáo ", "cập nhật thứ tự");

                    }
                    else
                    {
                        // Show system error
                        msg = new Message(MessageConstants.E0007, MessageType.Error);
                    }
                }
            }
            catch (Exception)
            {
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }

            return msg;
        }
        public Message MoveDown(Advertising objUI)
        {
            Message msg = null;
            try
            {
                if (objUI != null)
                {
                    // Get current group in dbContext
                    Advertising objCurrent = objUI;//GetByOrder(objUI.AdvertisingOrder);
                    Advertising objPrevious = GetByOrder(objUI.AdOrder - 1);

                    if (objCurrent != null && objPrevious != null)
                    {
                        int currentOrder = objCurrent.AdOrder;
                        int previousOrder = objPrevious.AdOrder;
                        // Update info by objUI
                        objCurrent.AdOrder = previousOrder;
                        objPrevious.AdOrder = currentOrder;

                        // Submit changes to dbContext
                        dbContext.SubmitChanges();
                        // Show success message
                        msg = new Message(MessageConstants.I0001, MessageType.Info, "Bài quảng cáo ", "cập nhật thứ tự");

                    }
                    else
                    {
                        // Show system error
                        msg = new Message(MessageConstants.E0007, MessageType.Error);
                    }
                }
            }
            catch (Exception)
            {
                // Show system error
                msg = new Message(MessageConstants.E0007, MessageType.Error);
            }

            return msg;
        }
        public int GetLastOrder()
        {
            int lastOrder = 1;
            try
            {
                lastOrder = dbContext.Advertisings.OrderByDescending(c => c.AdOrder).Select(c => c.AdOrder).FirstOrDefault<int>();
            }
            catch (Exception)
            {

            }
            return lastOrder;
        }
    }
}